package com.pmsystem.controller;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.pmsystem.service.FileService;
import com.pmsystem.util.TableResponse;

@Controller
public class FileController {

	@Autowired
	private FileService fileService;

	@RequestMapping(value = "admin/upload", method = RequestMethod.POST)
	@ResponseBody
	public Object uploadFile(HttpServletRequest request, MultipartFile file) throws IOException {
		// file.transferTo(new File(file.getOriginalFilename()));
		String fileName = file.getOriginalFilename();
		if (fileName == null || fileName.trim().equals("")) {
			return "error";
		}
		Date date = new Date();
		String today = new SimpleDateFormat("yyyy-MM-dd").format(date);
		// 上传文件摆放路径
		String url = request.getServletContext().getRealPath("/upload/" + today);
		// 创建文件夹
		File file1 = new File(url);
		if (!file1.exists()) {
			file1.mkdirs();
		}
		FileCopyUtils.copy(file.getBytes(), new File(url + "/" + fileName));
		boolean flag = fileService.uploadFile(url, fileName, date);
		String msg = null;
		if (flag) {
			msg = "success";
		} else {
			msg = "error";
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("msg", msg);
		return map;
	}

	@RequestMapping(value = "admin/download", method = RequestMethod.POST)
	public void download(HttpServletResponse response, @RequestParam Map<String, Object> map) throws IOException {
		String url = fileService.download(map);
		File file = new File(url);
		if (!file.exists() && !file.isFile()) {
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write("<script type='text/javascript'>alert('下载失败');</script>");
		}else {
			//转码，免得文件名中文乱码  
            String filename = URLEncoder.encode(file.getName(),"UTF-8"); 
            //设置文件下载头  
            response.addHeader("Content-Disposition", "attachment;filename=" + filename);    
            //1.设置文件ContentType类型，这样设置，会自动判断下载文件类型    
            response.setContentType("multipart/form-data"); 
			FileCopyUtils.copy(Files.newInputStream(file.toPath()), response.getOutputStream());
		}
	}

	@RequestMapping(value = "admin/findFile", method = RequestMethod.GET)
	@ResponseBody
	public Object findFile(@RequestParam Map<String, Object> map) {
		Long count = fileService.countFile(map);
		return new TableResponse(0, null, count, fileService.findFile(map));
	}

	@RequestMapping(value = "admin/updateFile", method = RequestMethod.POST)
	@ResponseBody
	public Object updateFile(@RequestParam Map<String, Object> map) {
		boolean flag = fileService.update(map);
		String msg = null;
		if (flag) {
			 msg = "success";
		}else {
			msg = "error";
		}
		Map<String, Object> map1 = new HashMap<>();
		map1.put("msg", msg);
		return map1;
	}

	@RequestMapping(value = "admin/deleteFile", method = RequestMethod.POST)
	@ResponseBody
	public Object deleteFile(Integer fileId) {
		boolean flag = fileService.deleteFile(fileId);
		String msg = null;
		if (flag) {
			 msg = "success";
		}else {
			msg = "error";
		}
		Map<String, Object> map1 = new HashMap<>();
		map1.put("msg", msg);
		return map1;
	}

}
